package com.oristand.controller;

import com.oristand.pojo.vo.StoreProductVo;
import com.oristand.service.StoreProductService;
import com.oristand.util.ResultResponse;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 店铺拥有商品的接口
 * 用于解决店铺和商品的关联问题
 * example:
 * 一个店铺有多个商品
 * 一个商品被多个店铺售卖
 *
 * @author shiyjian
 * @version 1.0
 * @date 2021/12/2 9:18
 */
@RestController
@CrossOrigin
public class StoreProductController {

    private StoreProductService storeProductService;

    private final static Integer[] STATUS_ARR = new Integer[]{1, 2, 3};

    @Autowired
    public void setStoreProductService(StoreProductService storeProductService) {
        this.storeProductService = storeProductService;
    }

    /**
     * 查询一个店铺有多少个商品信息
     * /store/{storeId}/productList/{status}
     *
     * @param storeId 店铺id
     * @param status  status是可选条件,1或null:查询所有商品,2:查询在售商品,3:查询下架商品
     * @return 返回ResultResponse集合对象
     * @see ResultResponse
     * @see StoreProductVo 一个店铺对象,对应多个商品对象
     */
    @RequestMapping(path = "/store/{storeId}/productList/{status}", method = RequestMethod.GET)
    public ResultResponse<StoreProductVo> findStoreProductVo(@PathVariable("storeId") Integer storeId,
                                                             @PathVariable("status") Integer status) {
        boolean flag = checkStatus(status);
        ResultResponse<StoreProductVo> resultResponse = new ResultResponse<>();
        if (flag) {
            StoreProductVo storeProductVo = storeProductService.findStoreProductList(storeId, status);
            resultResponse.selectSuccess(storeProductVo);
            if (storeProductVo != null) {
                return resultResponse;
            }
        } else {
            resultResponse.selectFail(ResultResponse.REQUEST_PARAMETER_ERROR);
        }
        return resultResponse;
    }

    /**
     * /store/{storeId}/count/{status}
     * 找出这个商店有多少商品,返回的是商品个数
     *
     * @param storeId 店铺id
     * @param status  状态码 status是可选条件,1或null:查询所有商品,2:查询在售商品,3:查询下架商品
     * @return 返回int值，个数
     * @see ResultResponse
     */
    @RequestMapping(path = "/store/{storeId}/count/{status}", method = RequestMethod.GET)
    public ResultResponse<Integer> findStoreProductCount(@PathVariable("storeId") Integer storeId,
                                                         @PathVariable("status") Integer status) {
        ResultResponse<Integer> resultResponse = new ResultResponse<>();
        boolean flag = checkStatus(status);
        if (flag) {
            int count = storeProductService.findStoreProductCount(storeId, status);
            resultResponse.selectSuccess(count);
        } else {
            resultResponse.selectFail(ResultResponse.REQUEST_PARAMETER_ERROR);
        }
        return resultResponse;
    }

    private boolean checkStatus(Integer status) {
        boolean flag = false;
        for (Integer item : STATUS_ARR) {
            if (status.intValue() == item.intValue()) {
                flag = true;
            }
        }
        return flag;
    }

}
